<?php include("../../includes/includes.php"); ?>
<?php
connect($config);
$partyTwo = $_POST['partyTwo'];
$partyTwoReceives = $_POST['partyTwoReceives'];
$partyOneReceives = $_POST['partyOneReceives'];

$loggedInTeam = getLoggedInTeam();
$partyTwoTeam = getTeamById($partyTwo, false);

$tradeId = createTrade($loggedInTeam->id, $partyTwoTeam->id, $loggedInTeam->leagueId);

if (strlen($partyTwoReceives) > 0) {
	$partyTwoReceives = substr($partyTwoReceives, 0, strlen($partyTwoReceives) - 1); 
}

if (strlen($partyOneReceives) > 0) {
	$partyOneReceives = substr($partyOneReceives, 0, strlen($partyOneReceives) - 1); 
}
$partyTwoReceivesItems = split(",", $partyTwoReceives);
$partyOneReceivesItems = split(",", $partyOneReceives);

$count = count($partyTwoReceivesItems);
for ($i = 0; $i < $count; $i++) {
	if (substr($partyTwoReceivesItems[$i], 0, 1) == "#") {
		createTradeItem($tradeId, substr($partyTwoReceivesItems[$i], 1), TradeItemType::Pick, PartyType::PartyTwo);	
	}
	else {
		createTradeItem($tradeId, $partyTwoReceivesItems[$i], TradeItemType::Player, PartyType::PartyTwo);
	}
}

$count = count($partyOneReceivesItems);
for ($i = 0; $i < $count; $i++) {
	if (substr($partyOneReceivesItems[$i], 0, 1) == "#") {
		createTradeItem($tradeId, substr($partyOneReceivesItems[$i], 1), TradeItemType::Pick, PartyType::PartyOne);	
	}
	else {
		createTradeItem($tradeId, $partyOneReceivesItems[$i], TradeItemType::Player, PartyType::PartyOne);
	}
}
mysql_close();

header( 'Location: displayTrades.php' ) ;
?>
